/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.ulb.f203.projet.vues;

import be.ulb.f203.projet.metier.Graphe;
import be.ulb.f203.projet.metier.Noeud;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.TitledBorder;

/**
 *
 * @author Srk
 */
public class GrapheFrame extends JFrame implements Observer {

    private Graphe g;
    private GrapheGui grapheGui;
    private JComboBox departCombo;
    private JComboBox arriveeCombo;
    private JRadioButton allNodesRadio;
    private JRadioButton shortestPathToRadio;
    private JPanel comboPanel;
    private JLabel departLabel;
    private JLabel arriveeLabel;
    private JLabel cheminLabel;

    /**
     * Crée une nouvelle interface graphique permettant de visualiser et
     * manipuler un graphe.
     *
     * @param graphe Graphe qui sera représenté graphiquement.
     */
    public GrapheFrame(Graphe graphe) {
        this.g = graphe;
        setTitle("CPS - GPS pour les étudiants");
        grapheGui = new GrapheGui(graphe);
        departCombo = new JComboBox<>(graphe.getNoeuds().toArray());
        arriveeCombo = new JComboBox<>(graphe.getNoeuds().toArray());
        allNodesRadio = new JRadioButton("Tous les noeuds");
        cheminLabel = new JLabel("", JLabel.CENTER);
        shortestPathToRadio = new JRadioButton("Chemin de racine vers...");
        departLabel = new JLabel("Racine", JLabel.CENTER);
        arriveeLabel = new JLabel("Vers", JLabel.CENTER);
        comboPanel = new JPanel(new GridLayout(0, 2));
        initComponents();
        graphe.addObserver(this);
    }

    private void initComponents() {

        JPanel jp = new JPanel();
        JPanel radioPanel = new JPanel(new GridLayout(0, 1));
        BorderLayout layout = new BorderLayout();

        ButtonGroup group = new ButtonGroup();
        group.add(shortestPathToRadio);
        group.add(allNodesRadio);

        allNodesRadio.setSelected(true);

        grapheGui.setDestNode((Noeud) arriveeCombo.getSelectedItem());
        departCombo.setSelectedItem(g.getRacine());

        jp.setLayout(new GridLayout(1, 1));
        TitledBorder title = BorderFactory.createTitledBorder("Panel de contrôle");
        jp.setBorder(title);
        radioPanel.add(allNodesRadio);
        radioPanel.add(shortestPathToRadio);

        jp.add(radioPanel);
        comboPanel.add(departLabel);
        comboPanel.add(departCombo);
        jp.add(comboPanel);

        // EVENTS
        allNodesRadio.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                displayPanel(e);
            }
        });
        shortestPathToRadio.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                displayPanel(e);
            }
        });
        departCombo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                changeRacine();
            }
        });
        arriveeCombo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                changeDest();
            }
        });

        setLayout(layout);
        add(grapheGui, BorderLayout.CENTER);
        add(cheminLabel, BorderLayout.NORTH);
        add(jp, BorderLayout.SOUTH);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    @Override
    public void update(Observable o, Object arg) {
        grapheGui.repaint();
    }

    private void changeDest() {
        grapheGui.setDestNode((Noeud) arriveeCombo.getSelectedItem());
        updateCheminLabel();
        grapheGui.repaint();
    }

    private void changeRacine() {
        Noeud racine = (Noeud) departCombo.getSelectedItem();
        g.setRacine(racine);
        updateCheminLabel();
    }

    private void displayPanel(ActionEvent e) {
        if (e.getSource().equals(allNodesRadio)) {
            comboPanel.remove(arriveeLabel);
            comboPanel.remove(arriveeCombo);
            grapheGui.setDisplayAllNodePath(true);

            updateCheminLabel();
            grapheGui.repaint();
        } else {
            comboPanel.add(arriveeLabel);
            comboPanel.add(arriveeCombo);
            grapheGui.setDisplayAllNodePath(false);

            updateCheminLabel();
            grapheGui.repaint();
        }
        comboPanel.revalidate();
        comboPanel.repaint();
    }

    private void updateCheminLabel() {
        if (allNodesRadio.isSelected()) {
            if (g.getPlusCourtCheminAllNodes() != null) {
                cheminLabel.setText(g.getPlusCourtCheminAllNodes().toString());
            }
        } else {
            if (g.getPlusCourtsChemins() != null) {
                cheminLabel.setText(g.getPlusCourtsChemins().getResultFor((Noeud) arriveeCombo.getSelectedItem()));
            }
        }
    }
}
